package com.css.dzj.dzjportal.modules.interflow.service.impl;

import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.modules.interflow.entity.AlInterflowEntity;
import com.css.dzj.dzjportal.modules.interflow.entity.QAlInterflowEntity;
import com.css.dzj.dzjportal.modules.interflow.mapper.InterflowMapper;
import com.css.dzj.dzjportal.modules.interflow.service.InterflowService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 公众交流实现层
 * @author wt
 * @date 2020\3\10 0010
 */
@Service
public class InterflowServiceImpl implements InterflowService {

    @Autowired
    private InterflowMapper iMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private HttpServletRequest request;

    /**
     * 添加公众交流信息
     */
    @Override
    @Transactional
    public void add(AlInterflowEntity interflowEntity) {

        try {
           // interflowEntity.setCreateUser(ServiceUtil.getLoginName(request));
            //interflowEntity.setId(SnowflakeId.getId());
            interflowEntity.setDelFlag(Constant.NO_DELETE);
            interflowEntity.setCreateTime(new Date());
            interflowEntity.setStatte("0");
            iMapper.save(interflowEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 修改公众交流信息
     */
    @Override
    @Transactional
    public void update(AlInterflowEntity interflowEntity) {

    }

    /**
     * 删除公众交流信息
     */
    @Override
    @Transactional
    public Boolean delete(String id) {
        if(StringUtils.isEmpty(id)){
            throw new RuntimeException("参数为空");
        }
        QAlInterflowEntity qEntity = QAlInterflowEntity.alInterflowEntity;
        try{
            jpaQueryFactory
                    .update(qEntity)
                    .set(qEntity.delFlag, Constant.DELETE)
                    .where(
                            qEntity.id.eq(id),
                            qEntity.delFlag.eq(Constant.NO_DELETE)
                    )
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 通过id查询公众交流信息
     */
    @Override
    public AlInterflowEntity findById(String id) {

        if(StringUtils.isEmpty(id)){

            throw new RuntimeException("参数为空");
        }
        QAlInterflowEntity qEntity = QAlInterflowEntity.alInterflowEntity;

        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.id.eq(id),
                        qEntity.delFlag.eq(Constant.NO_DELETE)
                )
                .fetchOne();
    }

    /**
     *通过咨询建议查询公众交流信息
     */
    @Override
    public QueryResults<AlInterflowEntity> gzjlList(String flowSuggest, Pageable pageable) {

        QAlInterflowEntity qEntity = QAlInterflowEntity.alInterflowEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ("".equals(flowSuggest) || flowSuggest == null) ? predicate : ExpressionUtils.and(predicate, qEntity.flowSuggest.like("%"+flowSuggest+"%"));
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE)) ;

        QueryResults<AlInterflowEntity> interflowResults = jpaQueryFactory.selectFrom(qEntity)
                .where(
                        predicate
                )
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return interflowResults;
    }

    /**
     * 分页查询公众交流信息
     */
    @Override
    public QueryResults<AlInterflowEntity> queryPage(Pageable pageable) {
        QAlInterflowEntity qEntity = QAlInterflowEntity.alInterflowEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ExpressionUtils.and(predicate, qEntity.delFlag.eq(Constant.NO_DELETE)) ;

        QueryResults<AlInterflowEntity> interflowResults = jpaQueryFactory.selectFrom(qEntity)
                .where(
                        predicate
                )
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return interflowResults;
    }
}
